﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonTypes.TelefoneTableAdapters;
using CommonTypes.MoradaTableAdapters;
using CommonTypes;

namespace SI2_TrabalhoFinal {
    public partial class Contactos : Form {
        public int Nif {
            get { return int.Parse(lblNif.Text); }
            set { lblNif.Text = value.ToString(); }
        }
        public int NumSegSocial {
            //IdPessoa
            get { return int.Parse(lblSegSocial.Text); }
            set { lblSegSocial.Text = value.ToString(); }
        }
        public string Nome {
            get { return lblNome.Text; }
            set { lblNome.Text = value; }
        }
        public string Apelido {
            get { return lblApelido.Text; }
            set { lblApelido.Text = value; }
        }

        public Contactos() {
            InitializeComponent();
        }

        public Contactos(Form mdiParent) : this() {
            this.MdiParent = mdiParent;
        }

        private void Contactos_Load(object sender, EventArgs e) {
            RefreshTelefones();
            RefreshMorada();            
        }

        private void RefreshTelefones() {
            TelefoneTableAdapter telefoneAdapter = new TelefoneTableAdapter();
            Telefone.TelefoneDataTable dt = telefoneAdapter.GetData(this.NumSegSocial);

            dgvTelefone.DataSource = dt;           
            dgvTelefone.Columns[dt.IdPessoaColumn.ColumnName].Visible = false;
            dgvTelefone.Columns[dt.IdTipoContactoColumn.ColumnName].Visible = false;
        }
        private void RefreshMorada() {
            MoradaTableAdapter moradaAdapter = new MoradaTableAdapter();
            Morada.MoradaDataTable dt = moradaAdapter.GetData(this.NumSegSocial);

            dgvMorada.DataSource = dt;            
            dgvMorada.Columns[dt.IdPessoaColumn.ColumnName].Visible = false;
            dgvMorada.Columns[dt.IdTipoContactoColumn.ColumnName].Visible = false;
        }

        private void dgvMorada_RowValidating(object sender, DataGridViewCellCancelEventArgs e) 
        {
            if(dgvMorada.Rows[e.RowIndex].DataBoundItem == null) return;
            Morada.MoradaRow row = (Morada.MoradaRow)(dgvMorada.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
                        
            row.IdPessoa = this.NumSegSocial;
            row.IdTipoContacto = 2;           
            e.Cancel = false;
        }

        private void button3_Click(object sender, EventArgs e) {
            
        }

        private void btnCancelar_Click(object sender, EventArgs e) {

        }

        private void btnGuardar_Click(object sender, EventArgs e) {
            Telefone.TelefoneDataTable telefones = (Telefone.TelefoneDataTable)dgvTelefone.DataSource;

            TelefoneTableAdapter telefoneAdapter = new TelefoneTableAdapter();

            foreach(Telefone.TelefoneRow row in telefones) {
                if(row.RowState == DataRowState.Added) {
                    telefoneAdapter.Insert(row.Ordem, row.Numero, row.IdPessoa, row.IdTipoContacto);
                }
                if(row.RowState == DataRowState.Deleted) {
                    row.RejectChanges();
                    telefoneAdapter.Delete(row.Ordem, row.IdPessoa);
                }
                if(row.RowState == DataRowState.Modified) {
                    telefoneAdapter.Update(row.Ordem, row.Numero, row.IdPessoa, row.IdTipoContacto);
                }
            }

            Morada.MoradaDataTable moradas = (Morada.MoradaDataTable)dgvMorada.DataSource;

            MoradaTableAdapter moradaAdapter = new MoradaTableAdapter();

            foreach(Morada.MoradaRow row in moradas) {
                if(row.RowState == DataRowState.Added) {
                    moradaAdapter.Insert(row.Ordem, row.Rua, row.Numero, row.CodigoPostar, row.Cidade, row.Pais, row.IdPessoa, row.IdTipoContacto);
                }
                if(row.RowState == DataRowState.Deleted) {
                    row.RejectChanges();
                    moradaAdapter.Delete(row.Ordem, row.IdPessoa);
                }
                if(row.RowState == DataRowState.Modified) {
                    moradaAdapter.Update(row.Ordem, row.Rua, row.Numero, row.CodigoPostar, row.Cidade, row.Pais, row.IdPessoa, row.IdTipoContacto);
                }
            }
            Contactos_Load(null, null);
        }

        private void dgvTelefone_RowValidating(object sender, DataGridViewCellCancelEventArgs e) 
        {
            if(dgvTelefone.Rows[e.RowIndex].DataBoundItem == null) return;
            Telefone.TelefoneRow row = (Telefone.TelefoneRow)(dgvTelefone.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
                       
            row.IdPessoa = this.NumSegSocial;
            row.IdTipoContacto = 2;            
            e.Cancel = false;
        } 
    }
}



